/**
 * product分页
 */
$(function () {
	_tableshow();
});
function excel(){
	var path="../product/allProduct.do";
	window.open(path,"100","100");
	/*$.ajax({
		url:"../product/allProduct.do",
		type: "Post",
		success:function(data){
			window.open(data.msg,"700","400");
			alert(data);
		}
	});*/
}



function _tableshow(){
	var currentPage = 1;//起始页
	var size = 1;//每页显示条数
	var val = $("input[name='keywords']").val();//参数
    $.ajax({
    	url: "../product/getProductList.do",
    	datatype: 'json',
    	type: "Post",
    	data: {
    		page:currentPage,
    		size:size,
    		param:val
    	},
    	success: function (data) {
    		if (data != null) {
    			var html;
       			$.each(eval("(" + data + ")").list, function (index, item) { //遍历返回的json
       				html = html + "<tr>";
       				html = html + "<td class='center'>"+item.pid+"</td>";
       				if(item.ptype==1){
       					html = html + "<td class='center'>计时产品</td>";
       				}
       				if(item.ptype==2){
       					html = html + "<td class='center'>计次产品</td>";
       				}
       				if(item.ptype==3){
       					html = html + "<td class='center'>打包产品</td>";
       				}
       				html = html + "<td class='center'>"+item.pname+"</td>";
       				html = html + "<td class='center'><span id='aaaaa' data-rel='tooltip' title='"+item.plabel+"'>"+item.minPlabel+"</span></td>";
       				html = html + "<td class='center a_link'><a href='../redirect/product_productlist_lecturelist.do?lid="+item.lid+"'>讲座列表</a></td>";
       				html = html + "<td class='center'>"+item.pprice+"元</td>";
       				html = html + "<td class='center'>"+item.pdatetime+"</td>";
       				html = html + "<td class='center'>"+item.pedate+"个月</td>";
       				if(item.pstatus==0){
       					html = html + "<td class='center'><span class='label arrowed label-primary'><input id='hahaha'  onclick='productlist_status($(this));' type='button' value='关闭' style='border: none;background-color: #2283c5;font-size: 12px;line-height: 12px;color: #fff;' /></span></td>";
       				}
       				if(item.pstatus==1){
       					html = html + "<td class='center'><span class='label arrowed label-primary'><input id='hahaha'  onclick='productlist_status($(this));' type='button' value='停售' style='border: none;background-color: #2283c5;font-size: 12px;line-height: 12px;color: #fff;' /></span></td>";
       				}
       				if(item.pstatus==2){
       					html = html + "<td class='center'><span class='label arrowed label-primary'><input id='hahaha'  onclick='productlist_status($(this));' type='button' value='开放' style='border: none;background-color: #2283c5;font-size: 12px;line-height: 12px;color: #fff;' /></span></td>";
       				}
       				html = html + "<td class='center a_link'><a href='../redirect/product_productlist_subscribe.do?pid="+item.pid+"'>111人</a></td>";
       				if(item.is==1){
       					html = html + "<td class='center a_link'><a id='aaa' href='../redirect/product_productlist_binding.do?pid="+item.pid+"' data-if='"+item.is+"'>名单列表</a></td>";
       				}else if(item.is==0){
       					html = html + "<td class='center'><a id='aaa'style='color: #808080;' data-if='"+item.is+"'>名单列表</a></td>";
       				}
       				html = html + "<td><div class='center'>";
       				html = html + "<a href='javascript:;' class='blue tooltip-info no-hover-underline' data-rel='tooltip' data-original-title='编辑产品' onclick='modifyproduct($(this));'>";
       				html = html + "<i class='icon-pencil bigger-130'></i></a>";
       				html = html + "<a href='javascript:;' class='red tooltip-error no-hover-underline delete' data-rel='tooltip' data-original-title='删除产品' onclick='deleteproductlist($(this))'>";
       				html = html + "<i class='icon-trash bigger-130'></i></a>";
       				html = html + "</div></td>";
       				html = html + "</tr>";
       			});
       			$("#list").html(html);
    		var pageCount = eval("(" + data + ")").pageCount; // 取到pageCount的值(把返回数据转成object类型)
    		var currentPage = eval("(" + data + ")").CurrentPage; // 得到CurrentPage
    		var options = {
    				currentPage: currentPage, // 当前页数
    				totalPages: pageCount, // 总页数
    				itemTexts: function (type, page, current) {
    					switch (type) {
    					case "first":
    						return "首页";
    					case "prev":
    						return "上一页";
    					case "next":
    						return "下一页";
    					case "last":
    						return "末页";
    					case "page":
    						return page;
    					}
    				},// 点击事件，用于通过Ajax来刷新整个list列表
    				onPageClicked: function (event, originalEvent, type, page) {
    					$.ajax({
    						url: "../product/getProductList.do?page=" + page,
    						type: "Post",
    						data: {
    				    		page:page,
    				    		size:size,
    				    		param:val
    				    	},
    						success: function (data1) {
    							if (data1 != null) {
    								var html;
    								$.each(eval("(" + data1 + ")").list, function (index, item) { // 遍历返回的json
    									html = html + "<tr>";
    				       				html = html + "<td class='center'>"+item.pid+"</td>";
    				       				if(item.ptype==1){
    				       					html = html + "<td class='center'>计时产品</td>";
    				       				}
    				       				if(item.ptype==2){
    				       					html = html + "<td class='center'>计次产品</td>";
    				       				}
    				       				if(item.ptype==3){
    				       					html = html + "<td class='center'>打包产品</td>";
    				       				}
    				       				html = html + "<td class='center'>"+item.pname+"</td>";
    				       				html = html + "<td class='center'><span id='aaaaa' data-rel='tooltip' title='"+item.plabel+"'>"+item.minPlabel+"</span></td>";
    				       				html = html + "<td class='center a_link'><a href='../redirect/product_productlist_lecturelist.do?lid="+item.lid+"'>讲座列表</a></td>";
    				       				html = html + "<td class='center'>"+item.pprice+"元</td>";
    				       				html = html + "<td class='center'>"+item.pdatetime+"</td>";
    				       				html = html + "<td class='center'>"+item.pedate+"个月</td>";
    				       				if(item.pstatus==0){
    				       					html = html + "<td class='center'><span class='label arrowed label-primary'><input id='hahaha'  onclick='productlist_status($(this));' type='button' value='关闭' style='border: none;background-color: #2283c5;font-size: 12px;line-height: 12px;color: #fff;' /></span></td>";
    				       				}
    				       				if(item.pstatus==1){
    				       					html = html + "<td class='center'><span class='label arrowed label-primary'><input id='hahaha'  onclick='productlist_status($(this));' type='button' value='停售' style='border: none;background-color: #2283c5;font-size: 12px;line-height: 12px;color: #fff;' /></span></td>";
    				       				}
    				       				if(item.pstatus==2){
    				       					html = html + "<td class='center'><span class='label arrowed label-primary'><input id='hahaha'  onclick='productlist_status($(this));' type='button' value='开放' style='border: none;background-color: #2283c5;font-size: 12px;line-height: 12px;color: #fff;' /></span></td>";
    				       				}
    				       				html = html + "<td class='center a_link'><a href='../redirect/product_productlist_subscribe.do?pid="+item.pid+"'>111人</a></td>";
    				       				if(item.is==1){
    				       					html = html + "<td class='center a_link'><a id='aaa' href='../redirect/product_productlist_binding.do?pid="+item.pid+"' data-if='"+item.is+"'>名单列表</a></td>";
    				       				}else if(item.is==0){
    				       					html = html + "<td class='center'><a id='aaa'style='color: #808080;' data-if='"+item.is+"'>名单列表</a></td>";
    				       				}
    				       				html = html + "<td><div class='center'>";
    				       				html = html + "<a href='javascript:;' class='blue tooltip-info no-hover-underline' data-rel='tooltip' data-original-title='编辑产品' onclick='modifyproduct($(this));'>";
    				       				html = html + "<i class='icon-pencil bigger-130'></i></a>";
    				       				html = html + "<a href='javascript:;' class='red tooltip-error no-hover-underline delete' data-rel='tooltip' data-original-title='删除产品' onclick='deleteproductlist($(this))'>";
    				       				html = html + "<i class='icon-trash bigger-130'></i></a>";
    				       				html = html + "</div></td>";
    				       				html = html + "</tr>";							
    				       			});
    								$("#list").html(html);
    							}
    						}
    					});
    				}
    			};
          $('#product_table').bootstrapPaginator(options);
        }
      }
    });
}


// 产品列表_产品状态切换
function productlist_status(s){
	if(window.confirm('您将要改变产品状态，请谨慎操作！')){
	var chi = s.parent().parent().parent().children('td')[10].childNodes[0].dataset.if;
	console.log(chi);
	if(chi!=0){
		if(s.val()=="开放"){
			s.val("停售");
		}
		else if(s.val()=="停售"){
			s.val("开放");
		}
	}
	else{
		if(s.val()=="开放"){
			s.val("停售");
		}
		else if(s.val()=="停售"){
		 	s.val("关闭");
		}
		else if(s.val()=="关闭"){
			s.val("开放");
		}
	}
		return true;
	}
	else{
		return false;
	}
}

//产品列表——编辑
function modifyproduct(_this){
	var chi = _this.parent().parent().parent().children('td')[8];
	var state=$(chi).children('span').children('input').val();
	console.log(state);
	console.log(chi);
	if(state=="关闭"){
		window.location.href="../redirect/product_edit.do";
	}
	else{
		alert("产品为关闭状态下才可编辑！");
	}
	
}
//产品列表——删除
function deleteproductlist(_this){
	var chi = _this.parent().parent().parent().children('td')[8];
	var state=$(chi).children('span').children('input').val();
	console.log(state);
	console.log(chi);
	if(state=="关闭"){
		if(window.confirm('是否删除该产品？')){
			$(chi).parent().remove();
			return true;
		}
		else{
			return false;
		}
		
	}
	else{
		alert("产品为关闭状态下才可删除！");
	}

}